#include <iostream>
using namespace std;
int a[200001];
int l[200001];
int r[200001];
int res[200001];
void  insert(int now,int index,int x,int y){
    if(a[now]>a[index]){
        if(l[now]!=0){
            insert(l[now], index,x+1,y-1);
        }else{
            l[now]=index;
            if(res[y+100000]<a[index]){
            res[y+100000-1] = a[index];
            }
            
        }
    }else if(a[now]<a[index]){
         if(r[now]!=0){
            insert(r[now], index,x+1,y+1);
        }else{
            r[now]=index;
              if(res[y+100000]<a[index]){
                    res[y+100000+1] = a[index];
            }
            
        }
    }
}


int main(){
    int n;
    for(int i=0;i<=200000;i++){
    	a[i]=0;
    	l[i]=0;
    	r[i]=0;
    	res[i]=0;
	}
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=2;i<=n;i++){
        insert(1,i,0,0);
    }
    res[100000]=a[1] ; 
    for(int i=0;i<=200000;i++){
    if(res[i]!=0){
    cout<<res[i];
    }
    }
}
